Web Commons
[
class tree: Web Commons
] [
index: Web Commons
] [
all elements
]
Web Commons
Todo List
Packages:
Web Commons
Source for file view.php
Documentation is available at
view.php
<?php
/**
* This file groups classes pertaining to the "view" part of MVC.
*
*
@author
Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>
*
@license
http://www.opensource.org/licenses/mit-license.php
*
@version
0.1dev
*/
/**
* Holds several slots.
* A slot is a cross-template variable, like a block, a title or even an object.
*
*
@since
0.1
*/
class
SlotHolder
{
/**
* Contains slots indexed by names.
*
*
@var
array
*/
private
$slots
=
array
(
)
;
/**
* The name of the slot that has been started with
*
{@link SlotHolder::start()}
. Null if no slot is open.
*
*
@var
string
*/
private
$open
=
Null
;
/**
* Adds a slot.
* Slot $name must not exist.
*
*
@param
string
$name
name for this slot
*
@param
mixed
$value
value for this slot
*
@since
0.1
*/
public
function
add
(
$name
,
$value
)
{
$this
->
crashIfExists
(
$name
)
;
$this
->
slots
[
$name
]
=
$value
;
}
/**
* Returns True if a slot is opened.
*
*
@return
bool
*
@since
0.1
*/
public
function
isOpen
(
)
{
return
$this
->
open
!==
Null
;
}
/**
* Throws a SlotException if slot $name exists.
*
*
@throws
SlotException
*
@since
0.1
*/
public
function
crashIfExists
(
$name
)
{
if
(
$this
->
exist
(
$name
))
{
throw
new
SlotException
(
'Caught attempt to duplicate slot "'
.
$name
.
'"'
)
;
}
}
/**
* If a slot is open, terminates the output buffer and throws a
* SlotException.
*
*
@throws
SlotException
*
@since
0.1
*/
public
function
crashIfOpen
(
)
{
if
(
$this
->
isOpen
(
))
{
$this
->
terminateBuffer
(
)
;
throw
new
SlotException
(
'Slot "'
.
$this
->
open
.
'" has not been closed.'
)
;
}
}
/**
* Opens a slot. Its contents will be the text printed until a call to
*
{@link SlotHolder::close()}
is issued.
* No slot must be opened, and slot $name must not exist.
*
*
@param
string
$name
the name of this slot
*
@since
0.1
*/
public
function
open
(
$name
)
{
$this
->
crashIfOpen
(
)
;
$this
->
open
=
$name
;
ob_start
(
)
;
}
/**
* Stores the previously opened slot. Throws a SlotException if no slot is
* open.
*
*
@throws
SlotException
*
@since
0.1
*/
public
function
close
(
)
{
if
(
!
$this
->
isOpen
(
))
{
throw
new
SlotException
(
'No slot is open.'
)
;
}
$this
->
slots
[
$this
->
open
]
=
ob_get_contents
(
)
;
$this
->
terminateBuffer
(
)
;
}
/**
* Terminates the buffer and resets $open to Null.
* This must not be called unless a buffer has been opened by this template.
*/
private
function
terminateBuffer
(
)
{
ob_end_clean
(
)
;
$this
->
open
=
Null
;
}
/**
* Returns the value of slot $name if it exists,
* the default value otherwise.
*
*
@param
string
$name
*
@param
mixed
$default
*
@return
mixed
*
@since
0.1
*/
public
function
getOrDefault
(
$name
,
$default
)
{
return
array_get_default
(
$this
->
slots
,
$name
,
$default
)
;
}
/**
* Returns the slot $name.
* Throws a SlotException if this slot does not exist.
*
*
@param
string
$name
*
@throws
SlotException
*
@return
mixed
*
@since
0.1
*/
public
function
getOrCrash
(
$name
)
{
if
(
array_key_exists
(
$name
,
$this
->
slots
))
{
return
$this
->
slots
[
$name
]
;
}
else
{
throw
new
SlotException
(
'Required slot "'
.
$name
.
'" is missing.'
)
;
}
}
/**
* Returns True if slot $name exists, False otherwise.
*
*
@param
string
$name
*
@return
bool
*
@since
0.1
*/
public
function
exist
(
$name
)
{
return
array_key_exists
(
$name
,
$this
->
slots
)
||
$this
->
open
===
$name
;
}
}
/**
* Renders an associative array.
* A template can be used as many times as needed.
*
*
@since
0.1
*/
interface
Template
{
/**
* Renders the given object/value.
* $slots is injected in the template as '$slots'.
*
*
@param
mixed
$object
*
@since
0.1
*/
function
render
(
$vars
,
SlotHolder
$slots
=
Null
)
;
}
/**
* Represents a template described by a PHP/HTML file.
*
*
@since
0.1
*/
class
FileBasedTemplate
implements
Template
{
private
$filename
;
/**
*
@param
string
$filename
path to template file
*/
public
function
__construct
(
$filename
)
{
if
(
is_file
(
$filename
))
{
$this
->
filename
=
$filename
;
}
else
{
throw
new
InvalidArgumentException
(
'No such template: '
.
$filename
)
;
}
}
/**
* (non-PHPdoc)
*
@see
WebCommons/Template::render()
*/
public
function
render
(
$vars
,
SlotHolder
$slots
=
Null
)
{
ob_start
(
)
;
extract
(
$vars
)
;
include
(
$this
->
filename
)
;
return
ob_get_clean
(
)
;
}
}
/**
* Exception thrown when an errors occurs with slots.
*
*
@since
0.1
*/
class
SlotException
extends
Exception
{
}
Documentation generated on Fri, 16 Jul 2010 00:48:40 +0200 by
phpDocumentor 1.4.3